package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;


public class StudentDao {
    //静态变量共享
    private static Student[] arr = new Student[5];

    public boolean addStudent(Student stu) {
        boolean flag = false;
        int index = -1;
        //查找数组为空的索引
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == null) {
                index = i;
                break;
            }
        }
        //判断index是否为-1 不是 就添加
        if (index != -1) {
            arr[index] = stu;
            flag = true;

        }
        return flag;
    }

    public Student[] finAllStudent() {
        return arr;
    }

    public void deleteStudentById(String id) {
        int index=getIndex(id);
        arr[index]=null;
    }

    /**
     * @param id 学生id
     * @return   返回数组中存储该id的学生对象的索引
     */
    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null && arr[i].getId().equals(id)){
                index=i;
            }
        }
        return  index;
    }

    public void updateStudentById(Student stu, String id) {
        int index = getIndex(id);
        arr[index]=stu;
    }
}
